/* 
 * Copyright TLR Team, Inc. All Rights Reserved.
 */

/* global AMap */

TLR.controllers.controller("DashboardNavCtrl", function ($rootScope, $scope, $location, UserSrv) {
    var absUrl = $location.absUrl();
    var loginUrl = absUrl.substring(0, absUrl.indexOf("/", absUrl.indexOf("/", absUrl.indexOf("/", absUrl.indexOf("/") + 1) + 1) + 1)) + "/login.html";
    UserSrv.checkLogin(function (res) {
        if (res.code !== 0) {
            window.location.href = loginUrl;
        } else {
            $rootScope.userName = res.data.user_name;
            $rootScope.userType = res.data.user_type;
        }
    });
    $scope.logOff = function () {
        UserSrv.logoff(function (res) {
            window.location.href = loginUrl;
        });
    };

    window.onbeforeunload = function (evt) {
        $.ajax({
            async: false,
            type: "GET",
            url: "device/rest/user/unload",
            dataType: 'json',
            success: function () {
            }
        });
    };
})
.controller("DashboardDeviceViewCtrl", function ($rootScope, $scope, $routeParams, DeviceSrv, EBanDeviceSrv, DEVICE) {
    $rootScope.view = $routeParams.view;
    $rootScope.subview = $routeParams.subview === 'all' ? 'purifier' : $routeParams.subview;
    $scope.device = new Device();
    $scope.onDeviceSave = function () {
        if ($scope.device_action === "add") {
            DeviceSrv.add($scope.device, function (resp) {
                if (resp.returncode === 0) {
                    console.log("add device successfully!");
                    $scope.$broadcast(DEVICE.event.list);
                } else {
                    alert(resp.message);
                }
            }, function (error) {
                alert(error);
            });
        } else if ($scope.device_action === "edit") {
            DeviceSrv.modify($scope.device, function (resp) {
                if (resp.returncode === 0) {
                    $scope.$broadcast(DEVICE.event.list);
                } else {
                    alert(resp.message);
                }
            }, function (error) {
                alert(error);
            });
        }
    };
    $scope.onDeviceToolbarClick = function (action) {
        $scope.device_action = action;
        if (action === "add") {
            $scope.device = new Device();
        } else if ($scope.device_action === "update_urgent_contact") {
            EBanDeviceSrv.update_urgent(function (res) {
                if (res.code === 0)
                    alert("更新成功");
                else
                    alert(res.message);
            });
        }
    };
    $scope.deviceDeleteMode = function (mode) {
        $scope.deleteInfo = {mode: mode};
    };
    $scope.bindUserDeleteMode = function (mode, userid, devicemac) {
        $scope.deleteInfo = {mode: mode, userid: userid, devicemac: devicemac};
    };
    $scope.relationDeleteMode = function (mode, relationid, devicemac) {
        $scope.deleteInfo = {mode: mode, relationid: relationid, devicemac: devicemac};
    };
    $scope.confirmDelete = function () {
        if ($scope.deleteInfo.mode === 'device') {
            DeviceSrv.del($scope.device, function () {
                $scope.$broadcast(DEVICE.event.list);
            });
        } else if ($scope.deleteInfo.mode === 'binduser') {
            DeviceSrv.del_bind({device_mac: $scope.deleteInfo.devicemac, user_id: $scope.deleteInfo.userid}, function (res) {
                if (res.code === 0) {
                    alert("删除成功");
                    $scope.$broadcast(DEVICE.event.bind_user_list, $scope.deleteInfo.devicemac);
                } else {
                    alert(res.message);
                }
            });
        } else if ($scope.deleteInfo.mode === 'delrelation') {
            EBanDeviceSrv.del_relation({
                id: $scope.deleteInfo.relationid,
                code: $scope.deleteInfo.devicemac
            }, function (res) {
                $scope.$broadcast(DEVICE.event.relation_list, $scope.deleteInfo.devicemac);
            });
        }
    };
    $scope.onDeviceSelect = function (device) {
        $scope.device = JSCopy(device);
        $scope.$broadcast(DEVICE.event.bind_user_list, device.devicemac);
        if ($rootScope.subview === 'health')
            $scope.$broadcast(DEVICE.event.relation_list, device.devicemac);
    };
    $scope.$on(DEVICE.event.relation_list, function (evt, code) {
        EBanDeviceSrv.alert_relations({code: code}, function (res) {
            if (res.code === 0) {
                $scope.relations = res.data;
            } else {
                $scope.relations = undefined;
            }
        });
         EBanDeviceSrv.tlr_urgent_relations({code: code}, function (res) {
            if (res.code === 0) {
                $scope.tlr_relations = res.data;
            } else {
                $scope.tlr_relations = undefined;
            }
        });
    });
    $scope.$on(DEVICE.event.bind_user_list, function (evt, devicemac) {
        DeviceSrv.binded_user({device_mac: devicemac}, function (res) {
            if (res.code === 0) {
                $scope.bindusers = res.data;
            } else {
                $scope.bindusers = undefined;
                alert(res.message);
            }
            console.log(res);
        });
    });
    $scope.$on(DEVICE.event.list, function () {
        var device_type = $rootScope.subview === 'health' ? 1 : ($rootScope.subview === 'measure' ? 3 : ($rootScope.subview === 'linkcard' ? 2 : ($rootScope.subview === 'purifier' ? 4 : 1)));
        DeviceSrv.list({device_type: device_type}, function (resp) {
            $scope.devices = resp;
        });
    });
    $scope.$broadcast(DEVICE.event.list);
})
.controller("DashboardDocViewCtrl", function ($rootScope, $scope, $routeParams, DocManageSrv) {
    $rootScope.view = $routeParams.view;
    DocManageSrv.list(function (ret) {
        if (ret.code === 0)
            $scope.docList = ret.data;
        else
            alert("获取文档列表失败");
    });
})
.controller("DashboardUserViewCtrl", function ($rootScope, $scope, $routeParams, UserSrv, USER) {
    $rootScope.view = $routeParams.view;
    $rootScope.subview = $routeParams.subview === 'all' ? 'customer' : $routeParams.subview;
    $scope.onUserToolbarClick = function (action) {
        $scope.user_action = action;
        if (action === "add") {
            $scope.user = new UserInfo();
        }
    };
    $scope.confirmDelete = function () {
        UserSrv.delete($scope.user, function (resp) {
            if (resp.code !== 0)
                alert(resp.message);
            else
                $scope.$broadcast(USER.event.list);
        });
    };
    $scope.onUserChangePasswordSave = function (newpassword, confirmpassword) {
        if (!newpassword || !confirmpassword) {
            alert("密码不能为空");
        } else if (newpassword !== confirmpassword) {
            alert("两次密码不一致");
        } else {
            $("#UserPasswordModal").modal('hide');
        }

    };
    $scope.onUserSelect = function (user) {
        $scope.user = JSCopy(user);
    };
    $scope.onUserSave = function () {
        if ($scope.user_action === "add") {
            UserSrv.register($scope.user, function (resp) {
                if (resp.code === 0) {
                    $scope.$broadcast(USER.event.list);
                } else {
                    alert("add user fail due to:" + resp.message);
                }
            });
        } else if ($scope.user_action === "edit") {
            UserSrv.update($scope.user, function (res) {
                if (res.code === 0)
                    alert("保存成功");
                else
                    alert(res.message);
                $scope.$broadcast(USER.event.list);
            }, function (resp) {
                if (resp.code !== 0)
                    alert(resp.message);
            });
        } else if ($scope.user_action === "changepassword") {

        }
    };
    $scope.$on(USER.event.list, function () {
        var userType = 0;
        if ($rootScope.subview === 'all' || $rootScope.subview === 'customer')
            userType = 1;
        else if ($rootScope.subview === 'employee')
            userType = 2;
        else if ($rootScope.subview === 'driver')
            userType = 3;
        else if ($rootScope.subview === 'urgent')
            userType = 4;
        UserSrv.list({type: userType}, function (resp) {
            if (resp.code === 0)
                $scope.users = resp.data;
            else {
                $scope.users = [];
                alert(resp.message);
            }
        });
    });
    $scope.$broadcast(USER.event.list);
})
.controller("DashboardStatisticViewCtrl", function ($rootScope, $scope, $routeParams, USER, EBanDeviceSrv) {
    $rootScope.view = $routeParams.view;
    $scope.$on(USER.event.list, function () {
        EBanDeviceSrv.all_health_user(function (healthResp) {
            if (healthResp.code !== 0) {
                alert(healthResp.message);
                return;
            }
            $scope.healthUsers = !healthResp.data ? [] : healthResp.data;
        });
    });

    $scope.onHealthUserSelect = function (editingHealthUser) {
        $scope.editingHealthUser = JSCopy(editingHealthUser);
    };
    $scope.onEditHealthUser = function () {
        EBanDeviceSrv.edit_health_user($scope.editingHealthUser, function (res) {
            if (res.code === 0)
                alert("保存成功");
            else
                alert(res.message);

            $scope.$broadcast(USER.event.list);
        });
    };
    $scope.$broadcast(USER.event.list);
});
